﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Taoyl.CodeTraceBLL;
using Taoyl.CodeTraceControlLib;
using Taoyl.CodeTraceHelper;
using Taoyl.CodeTraceModels;
using DayProduction = Taoyl.CodeTraceModels.DayProduction;
using MonthProduction = Taoyl.CodeTraceModels.MonthProduction;

namespace Taoyl.CodeTracePro
{
    public partial class FrmBoard : Form
    {
        public FrmBoard()
        {
            InitializeComponent();
            if (Global.SysInfo!=null)
            {
                this.production_Day.TotalProduction = Convert.ToInt32(Global.SysInfo.DayPlan);
                this.production_Month.TotalProduction = Convert.ToInt32(Global.SysInfo.MonthPlan);
            }
            UpdateProduction();
        }
        private HourProductionManager productionManager = new HourProductionManager();

        //曲线控件初始化
        #region  曲线控件初始化
        private void InitialChart()
        {
            //初始化月产量统计曲线
            #region 初始化月产量统计曲线
            this.chart_Month.Series.Clear();

            //添加一条曲线
            ChartHelper.AddSeries(this.chart_Month, "OK", SeriesChartType.Column,
                                    Color.FromArgb(54, 243, 177),
                                    Color.White,
                                    true);
            ChartHelper.AddSeries(this.chart_Month, "NG", SeriesChartType.Column,
                                   Color.Red,
                                   Color.White,
                                   true);

            //设置样式
            ChartHelper.SetStyle(this.chart_Month, Color.FromArgb(10, 29, 118), Color.White);

            //设置图例
            ChartHelper.SetLegend(this.chart_Month, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);

            //设置XY轴
            ChartHelper.SetXY(this.chart_Month, "", "", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.None, 1, 100);

            //设置刻度
            ChartHelper.SetMajorGrid(this.chart_Month, Color.Transparent, 1, 20);
            #endregion

            //初始化日产量统计曲线
            #region 初始化日产量统计曲线
            this.chart_Day.Series.Clear();

            //添加一条曲线
            ChartHelper.AddSeries(this.chart_Day, "OK", SeriesChartType.Column,
                                    Color.FromArgb(54, 243, 177),
                                    Color.White,
                                    true);
            ChartHelper.AddSeries(this.chart_Day, "NG", SeriesChartType.Column,
                                   Color.Red,
                                   Color.White,
                                   true);

            //设置样式
            ChartHelper.SetStyle(this.chart_Day, Color.FromArgb(10, 29, 118), Color.White);

            //设置图例
            ChartHelper.SetLegend(this.chart_Day, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);

            //设置XY轴
            ChartHelper.SetXY(this.chart_Day, "", "", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.None, 1, 100);

            //设置刻度
            ChartHelper.SetMajorGrid(this.chart_Day, Color.Transparent, 1, 20);
            #endregion

            #region 初始化月不良统计曲线
            this.chart_MonthNG.Series.Clear();

            //添加一条曲线
            ChartHelper.AddSeries(this.chart_MonthNG, "月不良统计", SeriesChartType.Pie,
                                    Color.FromArgb(54, 243, 177),
                                    Color.White,
                                    true);
            //设置样式
            ChartHelper.SetStyle(this.chart_MonthNG, Color.FromArgb(10, 29, 118), Color.White);

            //设置图例
            ChartHelper.SetLegend(this.chart_MonthNG, Docking.Top, StringAlignment.Far, Color.Transparent, Color.White);

            //设置XY轴
            ChartHelper.SetXY(this.chart_MonthNG, "", "", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.None, 1, 100);

            //设置刻度
            ChartHelper.SetMajorGrid(this.chart_MonthNG, Color.Transparent, 1, 20);
            #endregion

            #region 初始化日不良统计曲线
            this.chart_DayNG.Series.Clear();

            //添加一条曲线
            ChartHelper.AddSeries(this.chart_DayNG, "日不良统计", SeriesChartType.Pie,
                                    Color.FromArgb(54, 243, 177),
                                    Color.White,
                                    true);
            //设置样式
            ChartHelper.SetStyle(this.chart_DayNG, Color.FromArgb(10, 29, 118), Color.White);

            //设置图例
            ChartHelper.SetLegend(this.chart_DayNG, Docking.Top, StringAlignment.Far, Color.Transparent, Color.White);

            //设置XY轴
            ChartHelper.SetXY(this.chart_DayNG, "", "", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.None, 1, 100);

            //设置刻度
            ChartHelper.SetMajorGrid(this.chart_DayNG, Color.Transparent, 1, 20);
            #endregion
        }
        #endregion

        #region  数据统计初始化
        private int currentMonth = DateTime.Now.Month;
        private int currentDay = DateTime.Now.Day;

        private List<ProductionData> monthStatics = null;
        private List<ProductionData> dayStatics = null;
        private MonthProduction monthProduction = null;
        private DayProduction dayProduction = null;


        private void InitialData()
        {
            var monthStaticsResult = productionManager.GetMonthStatics(currentMonth);
            if (monthStaticsResult.IsSuccess)
            {
                monthStatics = monthStaticsResult.Content1;
                monthProduction = monthStaticsResult.Content2;
            }

            var dayStaticsResult = productionManager.GetDayStatics(currentMonth, currentDay);
            if (dayStaticsResult.IsSuccess)
            {
                dayStatics = dayStaticsResult.Content1;
                dayProduction = dayStaticsResult.Content2;
            }
        }
        #endregion

        #region  数据实时更新
        private void UpdateData()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(UpdateData));
            }
            else
            {
                //月产量
                if (this.monthStatics != null && this.monthProduction != null)
                {
                    //标题
                    this.production_Month.TitleName = currentMonth + "月产量统计";
                    this.lbl_ProductionMonth.Text = currentMonth + "月产量统计";
                    this.lbl_NGMonth.Text = currentMonth + "月不良统计";

                    //产量赋值
                    this.production_Month.CurrentProduction = monthProduction.MonthTotalCount;
                    this.meter_MonthOK.ActualValue = monthProduction.MonthOKCount;
                    this.meter_MonthOK.MaxValue = monthProduction.MonthTotalCount;
                    this.meter_MonthNG.ActualValue = monthProduction.MonthNGCount;
                    this.meter_MonthNG.MaxValue = monthProduction.MonthTotalCount;

                    //曲线赋值
                    this.chart_Month.Series[0].Points.Clear();
                    this.chart_Month.Series[1].Points.Clear();

                    for (int i = 0; i < monthStatics.Count; i++)
                    {
                        string xLable = this.monthStatics[i].DateTime;
                        this.chart_Month.Series[0].Points.AddXY(xLable, this.monthStatics[i].OkCount);
                        this.chart_Month.Series[0].Points[i].LabelForeColor = Color.White;
                        this.chart_Month.Series[1].Points.AddXY(xLable, this.monthStatics[i].NgCount);
                        this.chart_Month.Series[1].Points[i].LabelForeColor = Color.White;
                    }

                    this.chart_MonthNG.Series[0].Points.Clear();
                    this.chart_MonthNG.Series[0].Points.AddXY("良品", monthProduction.MonthOKCount);
                    this.chart_MonthNG.Series[0].Points.AddXY("不良品", monthProduction.MonthNGCount);
                    this.chart_MonthNG.Series[0].Points[0].Color = Color.FromArgb(54, 243, 177);
                    this.chart_MonthNG.Series[0].Points[0].LabelBackColor = Color.FromArgb(54, 243, 177);
                    this.chart_MonthNG.Series[0].Points[0].LabelForeColor = Color.Black;
                    this.chart_MonthNG.Series[0].Points[1].Color = Color.Red;
                    this.chart_MonthNG.Series[0].Points[1].LabelBackColor = Color.Red;
                    this.chart_MonthNG.Series[0].Points[1].LabelForeColor = Color.White;
                }
                //日产量
                if (this.dayStatics != null && this.dayProduction != null)
                {
                    //标题
                    this.production_Day.TitleName = currentDay + "日产量统计";
                    this.lbl_ProductionDay.Text = currentDay + "日产量统计";
                    this.lbl_NGDay.Text = currentDay + "日不良统计";
                    //产量赋值
                    this.production_Day.CurrentProduction = dayProduction.DayTotalCount;
                    this.meter_DayOK.ActualValue = dayProduction.DayOKCount;
                    this.meter_DayOK.MaxValue = dayProduction.DayTotalCount;
                    this.meter_DayNG.ActualValue = dayProduction.DayNGCount;
                    this.meter_DayNG.MaxValue = dayProduction.DayTotalCount;
                    //曲线赋值
                    this.chart_Day.Series[0].Points.Clear();
                    this.chart_Day.Series[1].Points.Clear();
                    for (int i = 0; i < dayStatics.Count; i++)
                    {
                        string xLable = this.dayStatics[i].DateTime;
                        this.chart_Day.Series[0].Points.AddXY(xLable, this.dayStatics[i].OkCount);
                        this.chart_Day.Series[0].Points[i].LabelForeColor = Color.White;
                        this.chart_Day.Series[1].Points.AddXY(xLable, this.dayStatics[i].NgCount);
                        this.chart_Day.Series[1].Points[i].LabelForeColor = Color.White;
                    }
                    this.chart_DayNG.Series[0].Points.Clear();
                    this.chart_DayNG.Series[0].Points.AddXY("良品", dayProduction.DayOKCount);
                    this.chart_DayNG.Series[0].Points.AddXY("不良品", dayProduction.DayNGCount);
                    this.chart_DayNG.Series[0].Points[0].Color = Color.FromArgb(54, 243, 177);
                    this.chart_DayNG.Series[0].Points[0].LabelBackColor = Color.FromArgb(54, 243, 177);
                    this.chart_DayNG.Series[0].Points[0].LabelForeColor = Color.Black;
                    this.chart_DayNG.Series[0].Points[1].Color = Color.Red;
                    this.chart_DayNG.Series[0].Points[1].LabelForeColor = Color.White;
                }
            }

        }
        #endregion

        #region  更新产量信息
        private void UpdateProduction()
        {
            InitialChart();
            InitialData();
            UpdateData();
        }
        #endregion

        #region  产品完成处理
        public void ProductCompletedHandle(bool result)
        {
            var sqlResult = productionManager.UpdateHourProduction(result);
            if (sqlResult.IsSuccess)
            {
                //更新
                //如果是当天，可以直接去更新对象，不用去查询数据
                //如果不是当天，不要更新，等待重新选择当天日期后更新
                if (DateTime.Now.Month == currentMonth && DateTime.Now.Day == currentDay)
                {
                    if (result)
                    {
                        dayProduction.DayOKCount += 1;
                        monthProduction.MonthOKCount += 1;

                        string date = DateTime.Now.Day + "日";
                        monthStatics.Where(m => m.DateTime == date).First().OkCount += 1;

                        date=$"{DateTime.Now.Hour}-{DateTime.Now.Hour+1}";
                        dayStatics.Where(d => d.DateTime == date).First().OkCount += 1;
                    }
                    else
                    {
                        dayProduction.DayNGCount += 1;
                        monthProduction.MonthNGCount += 1;

                        string date = DateTime.Now.Day + "日";
                        monthStatics.Where(m => m.DateTime == date).First().NgCount += 1;

                        date = $"{DateTime.Now.Hour}-{DateTime.Now.Hour + 1}";
                        dayStatics.Where(d => d.DateTime == date).First().NgCount += 1;
                    }
                    dayProduction.DayTotalCount = dayProduction.DayOKCount + dayProduction.DayNGCount;
                    monthProduction.MonthTotalCount = monthProduction.MonthOKCount + monthProduction.MonthNGCount;
                    UpdateData();
                }
            }
            else
            {

            }
        }
        #endregion

        #region  实时更新
        private DateTime lastTime = DateTime.Now;
        public void UpdateBoard()
        {
            //实时更新在线设备
            this.statics_Device.TotalCount = Global.A1A2.Count + Global.A3A4.Count + Global.A5A6.Count;
            this.statics_Device.OnLineCount=Global.A1A2.Where(d => d.IsConnected).Count()
                                        + Global.A3A4.Where(d => d.IsConnected).Count()
                                        + Global.A5A6.Where(d => d.IsConnected).Count();

            //如果跨天，重新初始化数据
            if (DateTime.Now.Day != lastTime.Day)
            {
                UpdateProduction();
                this.lastTime = DateTime.Now;
            }

        }


        #endregion

        #region  日期选择
        private void btn_SelectDay_Click(object sender, EventArgs e)
        {
            FrmDateSelect frmDateSelect = new FrmDateSelect(currentMonth, currentDay);

            if (frmDateSelect.ShowDialog() == DialogResult.OK)
            {
                currentMonth = frmDateSelect.SelectMonth;
                currentDay = frmDateSelect.SelectDay;
                InitialData();
                UpdateData();
            }
        }
        #endregion
        #region  目标产量设置
        private void btn_MonthPlan_Click(object sender, EventArgs e)
        {
            FrmMonthPlan frmMonthPlan = new FrmMonthPlan();
            if (frmMonthPlan.ShowDialog()==DialogResult.OK)
            {
                this.production_Month.TotalProduction = Convert.ToInt32(Global.SysInfo.MonthPlan);
            }
        }

        private void btn_DayPlan_Click(object sender, EventArgs e)
        {
            FrmDayPlan frmDayPlan = new FrmDayPlan();
            if (frmDayPlan.ShowDialog() == DialogResult.OK)
            {
                this.production_Day.TotalProduction = Convert.ToInt32(Global.SysInfo.DayPlan);
            }
        }
        #endregion
    }
}
